<template>
    <div>
        <!--对数据做查询-->
        <el-row>
            <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline">
                <el-form-item label="部门ID">
                    <el-input v-model="formInline.deptId" placeholder="部门ID"></el-input>
                </el-form-item>
                <el-form-item label="部门名称">
                    <el-input v-model="formInline.deptName" placeholder="部门名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
                <el-button
                        type="primary"
                        @click="handleAdd">添加部门
                </el-button>
            </el-form>
        </el-row>
        <!--表格内容-->
        <el-row>
            <el-table
                    :data="tableData"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="deptId"
                    border
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="deptId"
                        label="部门ID">
                </el-table-column>
                <el-table-column
                        prop="deptName"
                        label="部门名称">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="部门状态"
                        :formatter="deptStatus">
                </el-table-column>
                <el-table-column
                        prop="leader"
                        label="部长">
                </el-table-column>
                <el-table-column
                        prop="phone"
                        label="手机号">
                </el-table-column>
                <el-table-column
                        prop="email"
                        label="邮箱">
                </el-table-column>
                <el-table-column
                        prop="createBy"
                        label="创建者">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建日期">
                </el-table-column>
                <el-table-column
                        prop="updateBy"
                        label="更新者"
                >
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        label="更新时间">
                </el-table-column>
                <el-table-column
                        prop="remark"
                        label="备注">
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <!--编辑或添加对话框-->
        <el-dialog
                :title="title"
                :visible.sync="addDialogVisible"
                width="30%">
            <el-form :model="editOrAddForm" ref="editOrAddForm" :rules="rules" label-width="50px"
                     class="demo-ruleForm">
                <el-input v-model="editOrAddForm.deptId" style="display: none"></el-input>
                <el-input v-model="editOrAddForm.parentId" style="display: none"></el-input>
                <el-input v-model="editOrAddForm.ancestors" style="display: none"></el-input>
                <el-form-item label="名称" prop="deptName">
                    <el-input v-model="editOrAddForm.deptName"></el-input>
                </el-form-item>
                <el-form-item label="部长" prop="leader">
                    <el-input v-model="editOrAddForm.leader"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="phone">
                    <el-input v-model="editOrAddForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editOrAddForm.email"></el-input>
                </el-form-item>
                <el-form-item label="上级部门" prop="deptId">
                    <el-cascader
                            v-model="value"
                            :options="options"
                            :props="{ checkStrictly: true ,expandTrigger: 'hover' }"
                            @change="handleChange" style="width: 350px" clearable>
                    </el-cascader>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="editOrAddForm.status">
                        <el-radio :label="`0`" border>正常</el-radio>
                        <el-radio :label="`1`" border>停用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="editOrAddForm.remark"></el-input>
                </el-form-item>
            </el-form>
            <el-button @click="closeEditOrAddDialog">取 消</el-button>
            <el-button type="primary" @click="submitEditOrAddDialog('editOrAddForm')">确 定</el-button>
        </el-dialog>

    </div>
</template>
<script>
    export default {
        data() {
            const validateContract = (rule, value, callback) => {
                if (this.editOrAddForm.deptId === undefined) {
                    this.editOrAddForm.deptId = 0;
                }
                if (this.editOrAddForm.parentId === undefined) {
                    this.editOrAddForm.parentId = 0;
                }
                this.$http.get("/ss/dept/checkName?deptName=" + value + "&deptId=" + this.editOrAddForm.deptId + "&parentId=" + this.editOrAddForm.parentId)
                    .then((response) => {
                        if (response.data.resultCode === 5000) {
                            callback(new Error('该名称已存在！'));
                        } else {
                            callback();
                        }
                    })
            };
            return {
                tableData: [],
                formInline: {deptName: '', deptId: ''},
                addDialogVisible: false, //对话框的状态，默认关闭
                title: '',
                editOrAddForm: {
                    deptId: '',
                    deptName: '',
                    leader: '',
                    phone: '',
                    email: '',
                    status: '',
                    remark: '',
                    parentId: '',
                    ancestors: '',
                },
                value: [],
                options: [],
                rules: {
                    deptName: [
                        {required: true, message: '请输入部门名称', trigger: 'blur'},
                        {trigger: 'blur', validator: validateContract}//自定义验证:调方法
                    ],
                    status: [
                        {required: true, message: '请选择状态', trigger: 'blur'},
                    ],
                    leader: [
                        {required: true, message: '请输入部长名字', trigger: 'blur'},
                    ],
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                    ],
                    email: [
                        {required: true, message: '请输入邮箱', trigger: 'blur'},
                    ],
                },
            }
        },
        // 初始化数据  mounted()
        created: function () {
            this.loadData();
        },
        methods: {
            //封装数据请求
            loadData() {
                this.$http.post("/ss/dept/queryAll", this.formInline).then((response) => {
                    this.tableData = response.data.t;
                    this.total = response.data.t.total;
                })
            },
            //处理编辑
            async handleEdit(index, row) {
                let rowData = JSON.parse(JSON.stringify(row));
                this.value = [];
                this.editOrAddForm = {};
                this.title = '编辑部门信息';
                var _this = this;
                await this.$http.post("/ss/dept/queryAll", {}).then((response) => {
                    _this.options = response.data.t;
                });
                this.editOrAddForm = rowData;
                rowData.ancestors.split(",").forEach(e => {
                    this.value.push(Number(e))
                });
                this.value.splice(0, 1);
                this.addDialogVisible = true;
            },
            //处理删除
            handleDelete(index, row) {
            },
            //处理添加
            async handleAdd() {
                this.value = [];
                this.editOrAddForm = {};
                this.title = '添加部门信息';
                var _this = this;
                await this.$http.post("/ss/dept/queryAll", {}).then((response) => {
                    _this.options = response.data.t;
                });
                this.addDialogVisible = true;
            },
            //点击查询方法
            onSubmit() {
                this.currentPage = 1;
                this.loadData();
            },
            //判断部门状态
            deptStatus(row) {
                if (row.status === "0") {
                    return "正常"
                } else {
                    return "禁用"
                }
            },
            closeEditOrAddDialog() {
                this.addDialogVisible = false;
            },
            async submitEditOrAddDialog(editOrAddForm) {
                this.$refs[editOrAddForm].validate((valid) => {
                    this.editOrAddForm.parentId = this.value[this.value.length - 1];
                    this.editOrAddForm.ancestors = "0," + JSON.stringify(this.value).slice(1, -1);
                    if (valid) {
                        if (this.editOrAddForm.deptId >= 1) {
                            this.$http.put("/ss/dept/update", this.editOrAddForm).then((response) => {
                                if (response.data.resultCode === 2000) {
                                    this.$message({
                                        message: '修改成功！',
                                        type: 'success'
                                    });
                                    this.loadData();
                                    this.addDialogVisible = false;
                                } else {
                                    this.$message({
                                        message: response.data.resultMessage,
                                        type: 'error'
                                    });
                                }
                            })
                        } else {
                            this.$http.get("/ss/dept/checkName?deptName=" + this.editOrAddForm.deptName + "&deptId=" + this.editOrAddForm.deptId + "&parentId=" + this.editOrAddForm.parentId)
                                .then((response) => {
                                    if (response.data.resultCode === 5000) {
                                        this.$message({
                                            message: '当前部门名称重复！',
                                            type: 'error'
                                        });
                                    } else {
                                        this.$http.post("/ss/dept/insert", this.editOrAddForm).then((response) => {
                                            if (response.data.resultCode === 2000) {
                                                this.$message({
                                                    message: '添加成功！',
                                                    type: 'success'
                                                });
                                                this.loadData();
                                                this.addDialogVisible = false;
                                            } else {
                                                this.$message({
                                                    message: response.data.resultMessage,
                                                    type: 'error'
                                                });
                                            }
                                        })
                                    }
                                });

                        }
                    } else {
                        return false;
                    }
                });
            },
            //重置表单
            resetForm() {
                this.formInline = {};
                this.loadData();
            }, handleChange(value) {
            },


        }
    }
</script>
<style>

</style>